﻿using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using Bookvar.ServiceModel;

namespace Bookvar.UI.Adorners
{
    public class TopicAdorner : Adorner, IAdorner
    {
        private readonly UIElement adorningElement;
        private readonly AdornerPlacement placement;
        private readonly AdornerPosition position;

        public TopicAdorner( TopicContentPresenter presenter, UIElement adorningElement, AdornerPosition position,
                             AdornerPlacement placement ) : base( presenter )
        {
            this.adorningElement = adorningElement;
            if ( adorningElement != null )
            {
                AddVisualChild( adorningElement );

                //add the adorning element to presenter logical childs, so command bindings work correctly
                presenter.AddLogicalChild( adorningElement );
            }
            this.position = position;
            this.placement = placement;
        }

        protected override int VisualChildrenCount
        {
            get { return adorningElement == null ? 0 : 1; }
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            if (adorningElement != null)
            {
                Point adorningPoint = new Point();
                int pos = (int) position - 1;
                adorningPoint.X = ( ( ( pos % 3 ) / 2d ) * AdornedElement.DesiredSize.Width )
                                  - ( ( ( pos % 3 ) / 2d ) * adorningElement.DesiredSize.Width );
                adorningPoint.Y = ( ( ( pos / 3 ) / 2d ) * AdornedElement.DesiredSize.Height )
                                  - ( ( ( pos / 3 ) / 2d ) * adorningElement.DesiredSize.Height );
                if ( placement == AdornerPlacement.OutHorizontally )
                {
                    switch (pos % 3)
                    {
                        default:
                        case 1 :
                        {
                            //center column do nothing
                            break;
                        }
                        case 0 :
                        {
                            adorningPoint.X -= adorningElement.DesiredSize.Width;
                            break;
                        }
                        case 2 :
                        {
                            adorningPoint.X += adorningElement.DesiredSize.Width;
                            break;
                        }
                    }
                }
                else if ( placement == AdornerPlacement.OutVertically )
                {
                    switch (pos / 3)
                    {
                        default:
                        case 1:
                        {
                            //center row do nothing
                            break;
                        }
                        case 0:
                        {
                            adorningPoint.Y -= adorningElement.DesiredSize.Height;
                            break;
                        }
                        case 2:
                        {
                            adorningPoint.Y += adorningElement.DesiredSize.Height;
                            break;
                        }
                    }
                }

                adorningElement.Arrange( new Rect( adorningPoint, adorningElement.DesiredSize ) );
            }
            return finalSize;
        }

        protected override Visual GetVisualChild(int index)
        {
            if (index == 0 && adorningElement != null)
            {
                return adorningElement;
            }
            return base.GetVisualChild(index);
        }

        bool IAdorner.IsVisible
        {
            get
            {
                return IsVisible;
            }
        }
    }
}